package com.shr.exam.souhu;

import java.util.Scanner;

public class Main2 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int[] array = new int[num];
		for(int i = 0; i < num; i++)
			array[i] = sc.nextInt();
		System.out.println(getPalindrome1(array));
		sc.close();
	}
	
	
	public static int getPalindrome1(int[] array) {
		
		int[][] dp = getDP(array);
		int[] res = new int[array.length + dp[0][array.length - 1]];
		int i = 0;
		int j = array.length - 1;
		int resl = 0;
		int resr = res.length - 1;
		while (i <= j) {
			if (array[i] == array[j]) {
				res[resl++] = array[i++];
				res[resr--] = array[j--];
			} else if (dp[i][j - 1] < dp[i + 1][j]) {
				res[resl++] = array[j];
				res[resr--] = array[j--];
			} else {
				res[resl++] = array[i];
				res[resr--] = array[i++];
			}
		}
		int sum = 0;
		for(i = 0; i < res.length; i++) {
			sum += res[i];
		}
		return sum;
	}

	public static int[][] getDP(int[] array) {
		int[][] dp = new int[array.length][array.length];
		for (int j = 1; j < array.length; j++) {
			dp[j - 1][j] = array[j - 1] == array[j] ? 0 : 1;
			for (int i = j - 2; i > -1; i--) {
				if (array[i] == array[j]) {
					dp[i][j] = dp[i + 1][j - 1];
				} else {
					dp[i][j] = Math.min(dp[i + 1][j], dp[i][j - 1]) + 1;
				}
			}
		}
		return dp;
	}
}

